package no.ffi.gismoidm.utils;

//import dropinservice.ParameterClass;

import java.util.ArrayList;
import java.util.List;
import no.ffi.anf.gismoservice.ParameterClass1;
import no.ffi.gismoidm.discovery.DiscoveryQuery;
import no.ffi.gismoidm.discovery.DiscoveryRequest;
import no.ffi.gismoidm.discovery.DiscoveryResponse;
import no.ffi.gismoidm.discovery.ServiceItem;

//import nfficlient.NFFIRequest;

/**
 * Test client for debugging purposes
 * @author anders
 */



public class TestClient {
    static ClientProxy client;
    static String accessRequirements = "exists($SERVICE_PROVIDER)";
    public static DiscoveryResponse getDiscoveryInfo() throws Exception {      
        // Now call the test service
        DiscoveryQuery dq = new DiscoveryQuery();
        dq.accessRequirements = accessRequirements;
        dq.serviceTemplate = new ArrayList();
        dq.serviceTemplate.add("fongen.no");
        DiscoveryRequest dr = new DiscoveryRequest(dq);
        String accessReqs = "eq($DISCOVERY_PROVIDER,\"x\")";
        Object o = client.invokeApplicationService("DIRECTORY", dr, accessReqs);
        if (o instanceof Exception) throw (Exception)o;
        else System.out.println(o);
        return (DiscoveryResponse)o;
    }        
        
    public static void main(String[] args) throws Exception {
        System.out.println("Hei på deg");
        // Use the first found service and invoke it
        client = new ClientProxy("/home/anders/config/AndersFongen.properties");
        DiscoveryResponse resp = getDiscoveryInfo();
        List<ServiceItem> srvs = resp.serviceList;
        System.out.println("Antall returnerte tjenester: " + srvs.size());
        if (srvs.size()==0) System.out.println("Ingen tjenester ble funnet");
        else {
            ServiceItem si = srvs.get(0); // Get last
            Class parameter = si.getParameterClass();
            String url = si.serviceURL;
            ParameterClass1 pc = new ParameterClass1("abcd",109);
            while (true) {
                try {
                    Object o = client.invokeApplicationService("MAP", pc, accessRequirements);
                    System.out.println(o);
                    Thread.sleep(10000);
                } catch (ServiceInfoExpiredException e) {
                    resp = getDiscoveryInfo();
                    srvs = resp.serviceList;
                    si = srvs.get(0); // Get first
                }
            }
        }
    }
}
